package com.mall.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.commons.exceotion.BusinessException;
import com.mall.commons.exceotion.ErrorResult;
import com.mall.dubbo.mappers.*;
import com.tanhua.model.domain.*;

import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

@DubboService
public class UmsRoleApiImpl implements UmsRoleApi {

    @Autowired
    private UmsRoleMapper umsRoleMapper;

    @Autowired
    private UmsAdminRoleRelationMapper umsAdminRoleRelationMapper;

    @Autowired
    private UmsRoleMenuRelationMapper umsRoleMenuRelationMapper;

    @Autowired
    private UmsRolePermissionRelationMapper umsRolePermissionRelationMapper;

    @Autowired
    private UmsRoleResourceRelationMapper umsRoleResourceRelationMapper;

    @Autowired
    private UmsMenuMapper umsMenuMapper;

    @Override
    public List<UmsRole> finById(List role_id) {
        return umsRoleMapper.selectBatchIds(role_id);

    }

    @Override
    public List<UmsRole> roleListAll() {
        LambdaQueryWrapper<UmsRole> wrapper = new LambdaQueryWrapper<>();
        return umsRoleMapper.selectList(wrapper);
    }

    @Override
    public Page roleList(String keyword, Integer pageNum, Integer pageSize) {
        Page<UmsRole> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<UmsRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(keyword), UmsRole::getName, keyword);
        return umsRoleMapper.selectPage(page, wrapper);
    }

    //获取指定用户的角色
    @Override
    public List<UmsRole> adminId(Integer adminId) {
        if (adminId != null) {
            LambdaQueryWrapper<UmsAdminRoleRelation> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.in(UmsAdminRoleRelation::getAdminId, adminId);
            List<UmsAdminRoleRelation> list = umsAdminRoleRelationMapper.selectList(queryWrapper);
            List<Long> roleId = CollUtil.getFieldValues(list, "roleId", Long.class);
            if (roleId == null) {
                return null;
            }

            return umsRoleMapper.selectBatchIds(roleId);
        }
        return null;
    }

    /**
     * 给用户分配角色
     *
     * @param adminId
     * @param roleIds
     * @return
     */
    @Override
    public boolean roleUpdate(Integer adminId, Integer[] roleIds) {
        if (adminId == null) {
            return false;
        }
        //现根据amdinid查询
        LambdaQueryWrapper<UmsAdminRoleRelation> lqw = new LambdaQueryWrapper<>();
        lqw.in(UmsAdminRoleRelation::getAdminId, adminId);
        //先删除
        umsAdminRoleRelationMapper.delete(lqw);
        if (roleIds != null) {
            //在添加
            for (Integer roleId : roleIds) {
                UmsAdminRoleRelation relation = new UmsAdminRoleRelation();
                relation.setRoleId(Long.valueOf(roleId));
                relation.setAdminId(Long.valueOf(adminId));
                umsAdminRoleRelationMapper.insert(relation);
            }
        }
        return true;
    }

    /**
     * 获取角色相关菜单
     *
     * @param roleId
     * @return
     */
    @Override
    public List<UmsMenu> listMenu(Integer roleId) {
        LambdaQueryWrapper<UmsRoleMenuRelation> lqw = new LambdaQueryWrapper<>();
        lqw.eq(UmsRoleMenuRelation::getRoleId, roleId);
        List<UmsRoleMenuRelation> list = umsRoleMenuRelationMapper.selectList(lqw);
        List<Long> menu_id = CollUtil.getFieldValues(list, "menuId", Long.class);
        if (menu_id.size() == 0) {
            return null;
        }
        List<UmsMenu> umsMenus = umsMenuMapper.selectBatchIds(menu_id);
        return umsMenus;
    }

    /**
     * 给角色分配菜单
     *
     * @param roleId
     * @param menuIds
     * @return
     */
    @Override
    public boolean allocMenu(Integer roleId, Integer[] menuIds) {
        if (roleId == null) {
            return false;
        }
        //先删除在添加
        LambdaQueryWrapper<UmsRoleMenuRelation> lqw = new LambdaQueryWrapper<>();
        lqw.in(UmsRoleMenuRelation::getRoleId, roleId);
        umsRoleMenuRelationMapper.delete(lqw);
        if (menuIds != null) {
            for (Integer menuId : menuIds) {
                UmsRoleMenuRelation relation = new UmsRoleMenuRelation();
                relation.setMenuId(Long.valueOf(menuId));
                relation.setRoleId(Long.valueOf(roleId));
                umsRoleMenuRelationMapper.insert(relation);
            }
        }
        return true;
    }

    /**
     * 添加角色
     *
     * @param umsRole
     * @return
     */
    @Override
    public boolean create(UmsRole umsRole) {
        if (umsRole == null) {
            return false;
        }
        //先查询名字是否重复,重复则不允许操作
        LambdaQueryWrapper<UmsRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UmsRole::getName, umsRole.getName());
        if (umsRoleMapper.selectCount(wrapper) > 0) {
            throw new BusinessException("id重复请重新添加!");
        }
        umsRole.setCreateTime(new Date(System.currentTimeMillis()));
        umsRoleMapper.insert(umsRole);
        return true;
    }

    /**
     * 批量删除角色
     *
     * @param ids
     * @return
     */
    @Override
    public boolean deletes(Integer[] ids) {
        if (ids == null) {
            return false;
        }
        //查询其他表是否存在关联数据,存在不允许删除
        for (Integer id : ids) {
            //角色和菜单表
            LambdaQueryWrapper<UmsRoleMenuRelation> wrapper2 = new LambdaQueryWrapper<>(); //角色表
            wrapper2.in(UmsRoleMenuRelation::getRoleId, id);
            if (umsRoleMenuRelationMapper.selectCount(wrapper2) > 0) {
                throw new BusinessException(ErrorResult.serveError());
            }
            //角色和权限表
            LambdaQueryWrapper<UmsRolePermissionRelation> wrapper3 = new LambdaQueryWrapper<>(); //角色表
            wrapper3.in(UmsRolePermissionRelation::getRoleId, id);
            if (umsRolePermissionRelationMapper.selectCount(wrapper3) > 0) {
                throw new BusinessException(ErrorResult.serveError());
            }
            //角色和资源表
            LambdaQueryWrapper<UmsRoleResourceRelation> wrapper4 = new LambdaQueryWrapper<>(); //角色表
            wrapper4.in(UmsRoleResourceRelation::getRoleId, id);
            if (umsRoleResourceRelationMapper.selectCount(wrapper4) > 0) {
                throw new BusinessException(ErrorResult.serveError());
            }
            //删除角色表
            umsRoleMapper.deleteById(id);
        }

        return true;
    }

    /**
     * 获取角色相关资源
     *
     * @param roleId
     * @return
     */
    @Override
    public UmsRole UmsRole(Integer roleId) {
        return umsRoleMapper.selectById(roleId);
    }

    /**
     * 给角色分配资源
     *
     * @param roleId
     * @param resourceIds
     * @return
     */
    @Override
    public boolean allocResource(Integer roleId, Integer[] resourceIds) {
        if (resourceIds == null || roleId == null) {
            return false;
        }
        for (Integer resourceId : resourceIds) {
            UmsRoleResourceRelation relation = new UmsRoleResourceRelation();
            relation.setRoleId(Long.valueOf(roleId));
            relation.setResourceId(Long.valueOf(resourceId));
            umsRoleResourceRelationMapper.insert(relation);
        }
        return true;
    }

    /**
     * 修改角色状态
     *
     * @param status
     * @param id
     * @return
     */
    @Override
    public boolean updateStatus(Integer status, Integer id) {
        UmsRole umsRole = umsRoleMapper.selectById(id);
        if (umsRole != null) {
            umsRole.setStatus(status);
            umsRoleMapper.updateById(umsRole);
        }
        return true;
    }
}
